package com.boc.ewr.outsrc.service.impl;

import com.boc.ewr.bcimp.entity.OutsrcBillInfoCnfm;
import com.boc.ewr.bcimp.service.IOutsrcBillInfoCnfmService;
import com.boc.ewr.business.entity.ProPrbsdList;
import com.boc.ewr.outsrc.entity.OutsrcNdrDtl;
import com.boc.ewr.outsrc.mapper.OutsrcNdrDtlMapper;
import com.boc.ewr.outsrc.service.IOutsrcNdrDtlService;
import com.boc.ewr.util.MsgCategoryEnum;
import com.boc.ewr.util.ToUserUtil;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 退信明细（待确认信息清单）
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Service
public class OutsrcNdrDtlServiceImpl extends ServiceImpl<OutsrcNdrDtlMapper, OutsrcNdrDtl> implements IOutsrcNdrDtlService {

    @Autowired
    private OutsrcNdrDtlMapper outsrcNdrDtlMapper;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createInfo() {
        // 生成数据
        List<OutsrcNdrDtl> resultList = outsrcNdrDtlMapper.createInfo();

        if (resultList != null && resultList.size() > 0) {
            // 保存数据
            this.saveBatch(resultList);
            // 消息推送
            this.sendMsg(resultList);
        }
    }

    private void sendMsg(Collection<OutsrcNdrDtl> entityList) {
        // 获取推送用户
        Set<String> branchNoSet = new HashSet();
        for (OutsrcNdrDtl outsrcNdrDtl : entityList) {
            branchNoSet.add(outsrcNdrDtl.getOrgNo());
        }
        String toUser = ToUserUtil.genToUser(branchNoSet);
        // String toUser = "admin";
        if (toUser != null) {
            MessageDTO messageDTO = new MessageDTO(
                    "system",
                    toUser,
                    "外包管理-账单信息确认",
                    "待确认信息清单已更新，请及时进行反馈",
                    MsgCategoryEnum.BILL_MSG.getIndex()
            );
            sysBaseAPI.sendSysAnnouncement(messageDTO);
        }
    }
}
